﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using DAL;
using System.Text.RegularExpressions;

namespace PeisongWeb.admin
{
    public partial class customerUpdatePwdByAdmin : adminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("DK", 1))
                {
                    drpBind();
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }
        void drpBind()
        {
            DataTable dt = logic.sysCode.companyTypeList();
            ddl_Type.DataSource = dt;
            ddl_Type.DataBind();
            ddl_Type.Items.Insert(0, new System.Web.UI.WebControls.ListItem("--请选择--", ""));
        }
        void rptBind()
        {
            StringBuilder where = new StringBuilder();
            where.Append("isCheck=1");
            if (txt_companyName.Text.Trim() != "")
                where.AppendFormat("and company like '%{0}%'", txt_companyName.Text.Trim());
            if (ddl_Type.SelectedValue != "")
                where.AppendFormat("and type = '{0}'", ddl_Type.SelectedValue.Trim());
            if (txt_mobile.Text.Trim() != "")
                where.AppendFormat("and phone = '{0}'", txt_mobile.Text.Trim());
            DataSet ds = logic.customer.select(where.ToString(), "logintotal desc", AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex);
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            rpt_list.DataSource = ds.Tables[1];
            rpt_list.DataBind();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            rptBind();
        }
        protected void lbtn_Search_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            rptBind();
        }
        protected void rpt_list_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Update")
            {
                int customerid = Convert.ToInt32(e.CommandArgument);
                CustomerEntity cus = logic.customer.getById(customerid);
                txt_Username.Text = cus.Username;
                txt_Realname.Text = cus.Realname;
                txt_Phone.Text = cus.Phone;
                hid_customerid.Value = customerid.ToString();
                pwd_update.Show();
            }
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            if (chk_sendtophone.Checked)
            {
                if (Regex.IsMatch(txt_Phone.Text, @"^13[0-9]{1}[0-9]{8}|^15[0-3]{1}[0-9]{8}|^15[5-9]{1}[0-9]{8}|^18[0]{1}[0-9]{8}|^18[5-9]{1}[0-9]{8}$", RegexOptions.IgnoreCase))
                {
                    logic.customer.updatePwdByadmin(Convert.ToInt32(hid_customerid.Value.ToString()), Library.StringLbl.EncryptMD5(txt_Pwd.Text), Convert.ToInt32(hid_customerid.Value.ToString()), GetClientIP());
                    string content = string.Format("尊敬的采购配送用户，您的密码已被重置为{0}，请及时修改以确保交易安全。http://ps.ithinkgo.com",txt_Pwd.Text.Trim());
                    logic.sysAdmin.SendSMS(txt_Phone.Text, content, logic.sysAdmin.AdminID, Request.Url.ToString());
                    chk_sendtophone.Checked = false;
                    Library.Script.ClientMsg("密码修改成功!");
                    pwd_update.Close();
                }
                else
                {
                    Library.Script.ClientMsg("手机号码格式不正确！");
                    pwd_update.Show();
                }
            }
            else
            {
                logic.customer.updatePwdByadmin(Convert.ToInt32(hid_customerid.Value.ToString()), Library.StringLbl.EncryptMD5(txt_Pwd.Text),Convert.ToInt32(hid_customerid.Value.ToString()),GetClientIP());
                chk_sendtophone.Checked = false;
                Library.Script.ClientMsg("密码修改成功!");
                pwd_update.Close();
            }
        }
        protected void btn_Close_Click(object sender, EventArgs e)
        {
            pwd_update.Close();
        }
    }
}
